package com.fengba.nanzhi.model;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by duan on 2018/4/16.
 */


public class DataBaseHelper extends OrmLiteSqliteOpenHelper {

    private final static String DATABASE_NAME="mydb.db";

    private final static int VERSION = 1;//数据库的版本号

    private Map<String,Dao> maps = new HashMap<>();

    //使用单例模式
    private static DataBaseHelper instance;
    public  static  synchronized  DataBaseHelper getInstance(Context context){
        if(instance==null){
            synchronized (DataBaseHelper.class){
                if(instance == null){
                    instance = new DataBaseHelper(context);
                }
            }
        }
        return instance;
    }

    /**
     * 获取数据库访问对象
     * @param cls
     * @return
     * @throws SQLException
     */
    public synchronized Dao getDao(Class cls) throws SQLException{
        Dao dao = null;
        String className = cls.getSimpleName();
        if(maps.containsKey(className)){
            dao= maps.get(className);
        }else {
            dao = super.getDao(cls);
            maps.put(className,dao);
        }
        return dao;
    }

    public void close(){
        super.close();
        for (String key:maps.keySet()){
            Dao dao = maps.get(key);
            dao = null;
        }
    }

    public DataBaseHelper(Context context){
        super(context,DATABASE_NAME,null,VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        //完成对数据库的创建。和表的创建
        try {
            TableUtils.createTable(connectionSource,User.class);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) {
        try {
            TableUtils.dropTable(connectionSource,User.class,true);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
